{extend name="public:base" /}
{block name="content"}	
<div class="page-header">
	<h1>
		{$breadcrumb1}
		<small>
			<i class="ace-icon fa fa-angle-double-right"></i>
			{$breadcrumb2}
		</small>
		<small>
			<i class="ace-icon fa fa-angle-double-right"></i>
			{$crumbs}
		</small>			

	</h1>
</div>
	<div class="row">
	<div class="col-xs-12">	
		<div class="panel-body">
        <form action="{:url('Goods/edit_links')}" method="post" enctype="multipart/form-data" id="form-goods" class="form-horizontal">

          <ul class="nav nav-tabs">
          	
          	<li class="active"><a href="#tab-data" data-toggle="tab">折扣</a></li>
           
          </ul>
          <div class="tab-content">
          		
	       	<!-- 关联 START -->
	          	<div class="tab-pane active" id="tab-data">	          		
	         
	          		<div class="table-responsive">
	                	<table id="discount" class="table table-striped table-bordered table-hover">
							<thead>
								<tr>
								  <td class="text-left">数量</td>
								  <td class="text-right">价格</td>
								  <td></td>
								</tr>
							</thead>
							<tbody>
							<?php $discount_row = 0; ?>
		                     <?php if(isset($goods_discount)){ ?>
			                    <?php foreach ($goods_discount as $discount) { ?>
				                    <tr id="discount-row<?php echo $discount_row; ?>">
				                      <td class="text-left"><input type="text" name="goods_discount[<?php echo $discount_row; ?>][quantity]" value="<?php echo $discount['quantity']; ?>" class="form-control" /></td>
				                      <td class="text-left"><input type="text" name="goods_discount[<?php echo $discount_row; ?>][price]" value="<?php echo $discount['price']; ?>" class="form-control" /></td>
				                      <td class="text-left"> 
				                      	<input type="hidden" name="table" value="goods_discount" />
						
										<input type="hidden" name="goods_id" value="{$Request.param.id}" />
										
										<input type="hidden" name="pk_id" value="product_discount_id" />
										
										<input type="hidden" name="id" value="<?php echo $discount['product_discount_id']; ?>" />
										
										<input type="hidden" name="key" value="<?php echo $discount_row; ?>" />
				                      	
				                      	<button type="button" bid="discount-row<?php echo $discount_row; ?>" class="btn save btn-primary"><i class="fa fa-floppy-o"></i></button>
				                      	<button type="button" bid="discount-row<?php echo $discount_row; ?>" class="btn del btn-danger"><i class="fa fa-trash"></i></button>
				                      </td>
				                    </tr>
			                    <?php $discount_row++; ?>
			                    <?php } ?>
			                 <?php } ?>
							</tbody>
						</table>
		                <div><a  onclick="addDiscount();" class="add_image btn btn-primary ">添加折扣</a></div>
                  </div>
	          		
	          	
        		</div>
          </div>
        </form>
	</div>
	</div>
</div>
{/block}
{block name="javascript"}
<script>	
	var discount_row ={$discount_row|default='0'};

	function addDiscount() {
		html  = '<tr id="discount-row' + discount_row + '">'; 
		html += '  <td class="text-left"><input type="text" name="goods_discount[' + discount_row + '][quantity]" value="" class="form-control" /></td>';
		html += '  <td class="text-left"><input type="text" name="goods_discount[' + discount_row + '][price]" value="" class="form-control" /></td>';
		html += '  <td class="text-left">';
		
		html += '<input type="hidden" name="table" value="goods_discount" />';
  		
  		html += '<input type="hidden" name="goods_id" value="{$Request.param.id}" />';
  		
  		html += '<input type="hidden" name="pk_id" value="product_discount_id" />';
  							
		html += '<input type="hidden" name="id" value="" />';
		
		html += '<input type="hidden" name="key" value="' + discount_row + '" />';
		
		html += '<button type="button"  bid="discount-row' + discount_row + '"  class="btn save btn-primary"><i class="fa fa-floppy-o"></i></button>';
		html += '<button type="button"  bid="discount-row' + discount_row + '"  class="btn del btn-danger"><i class="fa fa-trash"></i></button>';
		html += '</td>';
		
		html += '</tr>';
		
		$('#discount tbody').append(html);
		
		discount_row++;
	}
	
$('.save').live('click', function() {
	
	var id='#'+$(this).attr('bid');	
	
	$.post(
		"{:url('Goods/ajax_eidt')}",
		$(id+' input[type=\'text\'],'+id+' input[type=\'hidden\']'),
		function(d){
			
			if(d.success){
				
				alert(d.success);
				
				if(d.id){
					$(id).find('input[name=\'id\']').val(d.id);
				}
				
			}else if(d.error){
				alert(d.error);
			}
		}
	);

});	
$('.del').live('click', function() {
	var f=confirm('确认要删除吗？');
	
	if(f==false){
		return false;
	}
	
	var id='#'+$(this).attr('bid');	
	
	$.post(
		"{:url('Goods/ajax_del')}",
		$(id+' input[type=\'hidden\']'),
		function(d){
			if(d.success){				
				alert(d.success);
				$(id).remove();
			}else if(d.error){
				alert(d.error);
				$(id).remove();
			}
		}
	);

});	
</script>
{/block}